<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="formRef" :model="form">
      <div class="tip">确定要为用户 {{ form.name }}({{ form.username }}) 重置密码吗？</div>
      <el-form-item label="新密码" required prop="password">
        <el-input
          class="max-w460"
          v-model="form.password"
          type="password"
          placeholder="请输入新密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" required prop="confirm_password">
        <el-input
          class="max-w460"
          v-model="form.confirm_password"
          type="password"
          placeholder="请输入确认密码"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确认重置</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { addManagerApi, resetPasswordApi, updateManagerApi } from "@/api/babycar";
import { ElMessage } from "element-plus";
import { inject, ref, watch } from "vue";
const title = ref("重置密码");
const dialogVisible = ref(false);
const form = ref({
  manager_id: 0,
  username: "",
  name: "",
  password: "",
  confirm_password: "",
});
const formRef = ref<any>(null);
const loading = ref(false);
const dialogFormVisible = () => {
  loading.value = false;
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (row?: any) => {
  form.value = {
    manager_id: row.manager_id,
    username: row.username,
    name: row.name,
    password: "",
    confirm_password: "",
  };
  dialogVisible.value = true;
};
const onSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      if (form.value.password != form.value.confirm_password) {
        ElMessage.error("密码不一致");
        return;
      }
      loading.value = true;
      resetPasswordApi({
        manager_id: form.value.manager_id,
        password: form.value.password,
      }).then((res) => {
        loading.value = false;
        ElMessage.success("重置成功");
        dialogFormVisible();
      });
    } else {
      return false;
    }
  });
};
/*取消*/
const cancelFunc = () => {
  loading.value = false;
  dialogFormVisible();
};
defineExpose({ open });
</script>

<style lang="scss" scoped>
.tip {
  font-size: 14px;
  padding: 10px 0;
}
</style>
